package fi.bluepill.server.rest;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.*;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;

public class BarcodeService extends BluepillService {
    public void image() {
        response.setContentType("image/png");

        int width = 200;
        int height = 200;

        String uuid = request.getParameter("uuid");

        //Query q = jpa().createQuery("select b from Barcode b where b.uuid = :uuid");
        //q.setParameter("uuid", uuid);

        //Barcode b = (Barcode)q.getSingleResult();

        //if (b == null) {
        //    error("No barcode found with the given uuid: " + uuid);
        //    return;
        //}

        byte[] contents = uuid.getBytes(Charset.forName("UTF-8"));
        QRCodeWriter codeWriter = new QRCodeWriter();

        ByteArrayOutputStream bos = new ByteArrayOutputStream();

        BufferedImage barcodeImage = null;

        try {
            BitMatrix bitMatrix = codeWriter.encode(new String(contents, Charset.forName("ISO-8859-1")), BarcodeFormat.QR_CODE, width, height);
            barcodeImage = MatrixToImageWriter.toBufferedImage(bitMatrix);
        } catch (WriterException e) {
            e.printStackTrace();
        }

        BufferedImage image = null;

        try {
            image = ImageIO.read(Thread.currentThread().getContextClassLoader().getResource("barcodecard2.jpg"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        int w = Math.max(image.getWidth(), barcodeImage.getWidth());
        int h = Math.max(image.getHeight(), barcodeImage.getHeight());
        BufferedImage combined = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);

        Image transparentImg = makeColorTransparent(barcodeImage, new Color(255,255,255));

        Graphics g = combined.getGraphics();
        g.drawImage(image, 0, 0, null);
        g.drawImage(transparentImg, 118, 208, null);

        try {
            OutputStream out = response.getOutputStream();
            ImageIO.write(combined, "PNG", out);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static Image makeColorTransparent(BufferedImage im, final Color color) {
        ImageFilter filter = new RGBImageFilter() {

            public int markerRGB = color.getRGB() | 0xFF000000;

            public final int filterRGB(int x, int y, int rgb) {
                if ((rgb | 0xFF000000) == markerRGB) {
                    return 0x00FFFFFF & rgb;
                } else {
                    return rgb;
                }
            }
        };

        ImageProducer ip = new FilteredImageSource(im.getSource(), filter);
        return Toolkit.getDefaultToolkit().createImage(ip);
    }
}
